package com.sim.simserial.dialog

import android.view.View
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.infinit.selfhelp.common.base.BaseDialogFragment
import com.infinit.selfhelp.common.navigation.HomeNavigator
import com.sim.simserial.R
import com.sim.simserial.databinding.DialogShoppingCartBinding
import com.sim.simserial.databinding.ItemShoppingCartBinding
import com.sim.simserial.navigation.MainNavigator
import com.sim.storage.GoodsBean
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class ShoppingCartDialogFragment : BaseDialogFragment<DialogShoppingCartBinding>() {

    @Inject
    lateinit var homeNavigator: MainNavigator

    companion object {
        const val GOODS = "goods"
    }

    override fun initViews(): View {
        return DialogShoppingCartBinding.inflate(layoutInflater).root
    }

    override fun initObserver() {
        super.initObserver()
        arguments?.getParcelableArrayList<GoodsBean>(GOODS)?.let {
            mBinding.rvShoppingCart.linear().setup {
                addType<GoodsBean>(R.layout.item_shopping_cart)
                onBind {
                    val binding = getBinding<ItemShoppingCartBinding>()
                    binding.img.setImageResource((getModel() as GoodsBean).urlId)
                    binding.tvPriceValue.text = (getModel() as GoodsBean).getPrice()
                }
            }.models = it

            mBinding.tvPriceValue.text = it.map { it.getPriceFloat() }.sum().toString()

            if (it.size > 3) {
                val layoutParams = mBinding.rvShoppingCart.layoutParams
                layoutParams.height = requireContext().resources.getDimension(com.infinit.selfhelp.common.R.dimen.dp_400).toInt()
                mBinding.rvShoppingCart.layoutParams = layoutParams
            }
        }
    }

    override fun initListeners() {
        mBinding.root.setOnClickListener {
            dismiss()
        }
        mBinding.cl.setOnClickListener { }
        mBinding.tvOrder.setOnClickListener {
            arguments?.getParcelableArrayList<GoodsBean>(GOODS)?.let {
                if (it.isNotEmpty()) {
                    homeNavigator.toOrderDetail(it)
                }
            }
        }
    }
}